package cn.amaake.magicplugin.nacos.util;

import java.lang.annotation.Annotation;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.env.PropertyResolver;
import org.springframework.util.StringUtils;

public class PropertiesPlaceholderResolver {
    private final PropertyResolver propertyResolver;

    public PropertiesPlaceholderResolver(PropertyResolver propertyResolver) {
        this.propertyResolver = propertyResolver;
    }

    public Properties resolve(Annotation annotation) {
        Map<String, Object> attributes = AnnotationUtils.getAnnotationAttributes(annotation);
        return this.resolve(attributes);
    }

    public Properties resolve(Map<?, ?> properties) {
        Properties resolvedProperties = new Properties();
        Iterator var3 = properties.entrySet().iterator();

        while(var3.hasNext()) {
            Map.Entry<?, ?> entry = (Map.Entry)var3.next();
            if (entry.getValue() instanceof CharSequence) {
                String key = String.valueOf(entry.getKey());
                String value = String.valueOf(entry.getValue());
                String resolvedValue = this.propertyResolver.resolvePlaceholders(value);
                if (StringUtils.hasText(resolvedValue)) {
                    resolvedProperties.setProperty(key, resolvedValue);
                }
            }
        }

        return resolvedProperties;
    }
}
